//
//  IMNotificationReceiver.m
//  IMChezhil
//
//  Created by zhangliucheng on 15/7/27.
//  Copyright (c) 2015年 CheZhil. All rights reserved.
//

#import "IMNotificationReceiver.h"
#import "IMCore.h"

#define OtherLoginNotification @"OtherLoginNotification"
#define MessageNotification @"MessageNotification"

@interface IMNotificationReceiver ()

@property (weak, nonatomic) id<IMNotificationReceiverDelegate> receiveDelegate;

@end

@implementation IMNotificationReceiver

- (instancetype)init {
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onReceiveOtherLogin:) name:OtherLoginNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onReceiveMessage:) name:MessageNotification object:nil];
    }
    return self;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:OtherLoginNotification object:Nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MessageNotification object:Nil];
}

- (void)onReceiveOtherLogin:(NSNotification *)notification {
    [[IMCore shareInstance] logout];
    [self.receiveDelegate OnOtherLoginBlock];
}

- (void)onReceiveMessage:(NSNotification *)notification {
    IMUserMessage *message = notification.object;
    [self.receiveDelegate OnMessageNotificater:message];
}

static IMNotificationReceiver *notificationReceiver;
+ (void)onRegisterNodification:(id<IMNotificationReceiverDelegate>)delegate {
    notificationReceiver = Nil;
    notificationReceiver = [IMNotificationReceiver new];
    notificationReceiver.receiveDelegate = delegate;
}

+ (void)postOtherLoginNotification {
    [[NSNotificationCenter defaultCenter] postNotificationName:OtherLoginNotification object:Nil];
}

+ (void)postMessageNotification:(IMUserMessage *)message {
    [[NSNotificationCenter defaultCenter] postNotificationName:MessageNotification object:message];
}
@end
